package izzat.fit.dbservice.service;

import com.fasterxml.jackson.databind.ser.Serializers;
import izzat.fit.dbservice.base.service.BaseService;
import izzat.fit.dbservice.entity.action.Action;
import izzat.fit.dbservice.entity.training.ActionSet;
import izzat.fit.dbservice.entity.training.Plan;
import izzat.fit.dbservice.mapper.ActionMapper;
import izzat.fit.dbservice.mapper.TrainingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName: TrainingService
 * @Description:
 * @Author: Izzat
 * @Date: 2019-12-29 18:28
 * @Version: v1.0
 */
@Service
public class TrainingService implements BaseService {
    @Autowired
    private TrainingMapper trainingMapper;
    @Autowired
    private ActionMapper actionMapper;

    public List<Plan> getAllPlan(){
        return trainingMapper.getAllPlan();
    }

    public Plan findPlanById(Integer integer) {
        return Optional.ofNullable(trainingMapper.findPlanById(integer)).orElse(new Plan());
    }

    public List<ActionSet> findActionSetByPlanId(Integer integer) {
        List<ActionSet> data =  getActionName(trainingMapper.findActionSetByPlanId(integer));
        return Optional.ofNullable(data).orElse(new ArrayList<>());
    }

    private List<ActionSet> getActionName(List<ActionSet> list){
        for (int i = 0; i < list.size(); i++){
            list.get(i).setActionName(actionMapper.findById(list.get(i).getAction()).getName());
        }
        return list;
    }

    public List<Plan> findPlanByCreator(int creator, int mode) {
        List<Plan> data;
        if(mode == 0)
            data =  trainingMapper.findPlanByCreator(creator);
        else
            data = trainingMapper.getAllPlan();
        return Optional.ofNullable(data).orElse(new ArrayList<>());
    }

    public int addPlan(Plan plan, List<ActionSet> actionSets){
        int newPlan = -1;
        plan.setCover("https://s1.ax1x.com/2020/03/17/8tn2xf.png");
        trainingMapper.addPlan(plan);
        newPlan = plan.getId();
        for(int i = 0; i < actionSets.size(); i++){
            actionSets.get(i).setPlan(newPlan);
            trainingMapper.addActionSet(actionSets.get(i));
        }
        if(newPlan != -1)
            return 1;
        else return 0;
    }

    public int deletePlanById(int[] list){
        String resultStr = "";
        List result = new ArrayList<Integer>();
        return trainingMapper.deletePlanById(list);
//        for(int i = 0; i < list.length; i++){
//            if(trainingMapper.deletePlanById(list[i]) == 0){
//                result.add(list[i]);
//            }
//        }
//        for(int i = 0; i < result.size(); i++){
//            resultStr = "计划";
//            if(i < result.size() - 1){
//                resultStr += (i + ", ");
//            }else
//                resultStr += (i + "删除失败");
//            return resultStr;
//        }
    }

    @Override
    public Object findById(Integer integer) {
        return null;
    }
}
